/* 
 *  Copyright 2012 CodeMagi, Inc.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.codemagi.servlets;

import java.util.Enumeration;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;

public final class ServletUtils {

    static Logger log = Logger.getLogger("com.codemagi.servlets.ServletUtils");

    /**
     * Logs the request attributes. The attribute "content" is skipped due to its size.
     */
    public static void logRequestAttributes(HttpServletRequest request) {
	log.debug("REQUEST ATTRIBUTES: ");

	Enumeration e = request.getAttributeNames();
	while (e.hasMoreElements()) {
	    String name  = (String)e.nextElement();
	    Object value = request.getAttribute(name);

	    if ( !("content".equals(name) || 
		   "password".equals(name) ||
		   "javax.servlet.request.ssl_session".equals(name) ) ) log.debug("    - " + name + " = " + value);
	}
    }

    
    /**
     * Logs the request cookies. 
     */
    public static void logRequestCookies(HttpServletRequest request) {
	log.debug("REQUEST COOKIES: ");

        Cookie cookies[] = request.getCookies();

	for (int i = 0; i < cookies.length; i++) {
	    Cookie cookie = cookies[i];

	    log.debug("    - " + cookie.getName() + " = " + cookie.getValue() + 
		      " DOMAIN: " + cookie.getDomain() + " PATH: " + cookie.getPath());
	}
    }

    
    /**
     * Logs the request parameters. 
     */
    public static void logRequestParameters(HttpServletRequest request) {
	log.debug("REQUEST PARAMETERS: ");

	Enumeration e = request.getParameterNames();
	while (e.hasMoreElements()) {
	    String name  = (String)e.nextElement();
	    Object value = request.getParameter(name);

	    if ( !("content".equals(name) || "password".equals(name) || 
		   "currentPass".equals(name) || 
		   "pass1".equals(name) || "pass2".equals(name)) ) log.debug("    - " + name + " = " + value);
	}
    }


    /**
     * Logs the request headers. 
     */
    public static void logRequestHeaders(HttpServletRequest request) {
	log.debug("REQUEST HEADERS: ");

	Enumeration e = request.getHeaderNames();
	while (e.hasMoreElements()) {
	    String name  = (String)e.nextElement();
	    Object value = request.getHeader(name);

	    if ( !("content".equals(name) || "password".equals(name)) ) log.debug("    - " + name + " = " + value);
	}
    }

    
    /**
     * Logs the session attributes. The attribute "content" is skipped due to its size.
     */
    public static void logSessionAttributes(HttpServletRequest request) {
	log.debug("SESSION ATTRIBUTES: ");

	HttpSession session = request.getSession();

	Enumeration e = session.getAttributeNames();
	while (e.hasMoreElements()) {
	    String name  = (String)e.nextElement();
	    Object value = session.getAttribute(name);

	    if (!"content".equals(name)) log.debug("    - " + name + " = " + value);
	}
    }

}
